package com.meisupic.copyright.service.impl;

import static com.meisupic.copyright.common.Constants.DISABLE;
import static com.meisupic.copyright.common.Constants.ENABLE;

import com.meisupic.copyright.common.Constants;
import com.meisupic.copyright.domain.po.SysDicPo;
import com.meisupic.copyright.domain.query.SysDicQuery;
import com.meisupic.copyright.domain.vo.SysDicVo;
import com.meisupic.copyright.exception.BizException;
import com.meisupic.copyright.repo.SysDicRepo;
import com.meisupic.copyright.service.ISysDicService;
import com.meisupic.copyright.support.EhcacheHelper;
import com.meisupic.copyright.util.BeanMapper;
import com.meisupic.copyright.util.BeanUtil;
import com.meisupic.copyright.util.StringUtil;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

/**
 * creation wuxun
 * @version V1.0
 * @Description: 数据字典接口实现类
 * @date 2018/1/14 9:50
 */
@Service
public class SysDicServiceImpl implements ISysDicService {

  @Autowired
  SysDicRepo sysDicRepo;

  @Override
  @CacheEvict(value = "sysDicCache", key = "#sysDicPo.type")
  public void save(SysDicPo sysDicPo) {
    if (sysDicPo.getId() == null) {
      if (StringUtil.isEmpty(sysDicPo.getEditable())) {
        sysDicPo.setEditable(Constants.DISEDITABLE);
      }
      sysDicPo.setCreateTime(new Timestamp(new Date().getTime()));
      sysDicRepo.save(sysDicPo);
    } else {
      SysDicPo sysDicPoExistEntity = sysDicRepo.getOne(sysDicPo.getId());
      if (sysDicPoExistEntity.getEditable() == DISABLE) {
        if (!sysDicPoExistEntity.getCode().equals(sysDicPo.getCode()) ||
            !sysDicPoExistEntity.getText().equals(sysDicPo.getText()) ||
            !sysDicPoExistEntity.getType().equals(sysDicPo.getType())) {
          throw new BizException("数据字典不允许编辑");
        }
      }

      BeanUtil.copyPropertiesIgnoreNull(sysDicPo, sysDicPoExistEntity);
      sysDicRepo.saveAndFlush(sysDicPoExistEntity);
    }

  }

  @Override
  public void delete(Long id) {
    SysDicPo po = sysDicRepo.getOne(id);
    SysDicPo sysDicPo = new SysDicPo(id);
    sysDicPo.setEditable(DISABLE);
    sysDicPo.setEnable(DISABLE);
    this.save(sysDicPo);
    EhcacheHelper.remove("sysDicCache", po.getType());
  }

  @Override
  public SysDicVo findById(Long id) {
    SysDicPo sysDicPo = sysDicRepo.findOne(id);
    SysDicVo sysDicVo = BeanMapper.map(sysDicPo, SysDicVo.class);
    return sysDicVo;
  }

  @Override
  @Cacheable(value = "sysDicCache", key = "#type")
  public Map<String, String> findEnableDic(String type) {
    SysDicPo sysDicPo = new SysDicPo(Constants.EDITABLE, Constants.ENABLE, type);
    Sort sort = getOrders();
    List<SysDicPo> sysDicPoList = sysDicRepo.findAll(Example.of(sysDicPo), sort);
    return getDicMap(sysDicPoList);
  }

  private Sort getOrders() {
    Sort sort = new Sort(Sort.Direction.DESC, "createTime");
    sort.and(new Sort("sortNo"));
    return sort;
  }

  @Override
  @Cacheable(value = "sysDicCache", key = "#type")
  public Map<String, String> findAllDic(String type) {
    SysDicPo sysDicPo = new SysDicPo(null, ENABLE, type);
    Sort sort = getOrders();
    List<SysDicPo> sysDicPoList = sysDicRepo.findAll(Example.of(sysDicPo), sort);
    return getDicMap(sysDicPoList);
  }

  private Map<String, String> getDicMap(List<SysDicPo> sysDicPoList) {
    Map<String, String> map = new HashMap<>(16);
    for (SysDicPo dicPo : sysDicPoList) {
      map.put(dicPo.getCode(), dicPo.getText());
    }
    return map;
  }


  @Override
  public Page<SysDicVo> findPage(SysDicQuery sysDicQuery, Pageable page) {
    SysDicPo sysDicPo = BeanMapper.map(sysDicQuery, SysDicPo.class);
    Page<SysDicPo> pageResult = sysDicRepo.findAll(Example.of(sysDicPo), page);
    List<SysDicPo> sysDicPoList = pageResult.getContent();
    List<SysDicVo> sysDicVoList = BeanMapper.mapList(sysDicPoList, SysDicVo.class);
    Page<SysDicVo> sysDicVoPage = new PageImpl<SysDicVo>(sysDicVoList, page,
        pageResult.getTotalElements());
    return sysDicVoPage;
  }
}
